<template>
	<view class="myAuth">
		<u--form labelPosition="left" :model="formData" :rules="rules" ref="form1">
			<view class="upImg">
				<view class="">
					上传公司照片
				</view>
				<imgUpload code="zsbm" placeholder="请上传图片" :limit="1" @upImgs="upImgs" ref="imgUpload" :imageArr="gacadeUp"></imgUpload>
			</view>
			<u-form-item label="公司(苗圃名称)"  borderBottom  labelWidth="120">
				<u--input v-model="formData.company_name" border="none" placeholder="请输入内容"></u--input>
			</u-form-item>
			<view class="upImg">
				<view class="">
					营业执照
				</view>
				<imgUpload code="zsbm" placeholder="请上传图片" :limit="1" @upImgs="upImgs1" ref="imgUpload1" :imageArr="businessUp"></imgUpload>
			</view>
			<u-form-item label="苗圃电话" prop="phone" borderBottom ref="item1" labelWidth="100">
				<u--input v-model="formData.phone" border="none" placeholder="请输入内容"></u--input>
			</u-form-item>
			<u-form-item label="面积(平米)" prop="area" borderBottom ref="item1" labelWidth="100">
				<u--input v-model="formData.area" border="none" placeholder="请输入面积"></u--input>
			</u-form-item>
			<u-form-item label="简介" prop="introduction" borderBottom ref="item1" labelWidth="100">
				<u--input v-model="formData.introduction" border="none" placeholder="请输入内容"></u--input>
			</u-form-item>
		</u--form>
		<view class="nursery" v-if="showNursery" style="margin-top: 10rpx;color: #979797;">
			提示：苗圃商家认证费用需要{{sysConfig.sys_config.nursery_certificat}}
		</view>
		<u-button class="custom-style" shape="circle" text="提交审核"
			customStyle="background-color: #00C777;color:#fff;margin-top:70px" @click="goAuth"></u-button>
		<u-modal :show="show" :title="title" :content='content' @confirm="confirm" showCancelButton="true"
			@cancel="cancel"></u-modal>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import imgUpload from '@/components/common/imgUpload.vue'
	import {
		weChatPay,
		systemConfig,
		getUser,
		companyAuth,
		myCompanyAuth
	} from '@/request/api.js'
	export default {
		components: {
			imgUpload
		},
		data() {
			return {
				show: false,
				gacadeUp:[],
				title: '提示',
				content: '',
				formData: {
					company_name: '',
					gacade_img: '', //门头照
					business_img: '', //营业照
					phone: '',
					area: '', //面积
					introduction: ''
				},
				ca_id: '',
				businessUp:[],
				sysConfig: null,
				is_nursery_certification:0,//苗圃是否成功0未成功，1成功
				showNursery: false, //提示是否需要支付
				nurseryPay: 0,
				rules: {
					'company_name': {
						type: 'string',
						required: false,
						message: '请填写公司名称',
						trigger: ['blur', 'change']
					}
				}
			}
		},
		methods: {
			getInfo() {
				myCompanyAuth().then(res => {
					if (res.code == 1) {
						this.formData.company_name = res.data.company_name
						this.formData.gacade_img = res.data.gacade_img //门头照
						this.formData.business_img = res.data.business_img //营业照
						// this.formData.business_img_url = res.data.business_img_url //展示地址
						// this.formData.gacade_img_url= res.data.gacade_img_url//展示地址
						this.formData.phone = res.data.phone
						this.formData.area = res.data.area //面积
						this.formData.introduction = res.data.introduction
						if(res.data.gacade_img!=''){
							this.gacadeUp.push({url:res.data.gacade_img_url})
						}
						if(res.data.business_img!=''){
							this.businessUp.push({url:res.data.business_img_url})
						}
					}
					// console.log(this.gacadeUp)
				})
			},
			cancel() { //取消
				this.show = false
			},
			pay() {
				let self = this
				weChatPay({
					amount: this.sysConfig.sys_config.nursery_certificat,
					type: 1,
					ca_id: this.ca_id
				}).then(res => {
					console.log(res)
					uni.requestPayment({
						"timeStamp": res.data.timeStamp,
						"nonceStr": res.data.nonceStr,
						"package": res.data.package,
						"signType": "MD5",
						"paySign": res.data.paySign,
						success: (res) => {
							self.show = false
							uni.navigateTo({
								url: '/my_pages/components/success?title=苗圃认证已提交请等待审核'
							})
							this.$refs.imgUpload.empt()
							this.$refs.imgUpload1.empt()
							this.emptForm()
							this.getConfig()
							this.getUser()
						}, // 支付成功
						fail: (err) => {}, // 支付失败
					})
				})

			},
			confirm() {
				this.pay()
			},
			upLoadData() {
				// console.log(this.formData)
				companyAuth(this.formData).then(res => {
					if (res.code == 1) {
						this.ca_id = res.data.ca_id
						if (this.nurseryPay != 0 || this.sysConfig.sys_config.nursery_certificat == 0) {
							//已缴费或者不需要认证费时候直接提交
							uni.navigateTo({
								url: '/my_pages/components/success?title=苗圃认证已提交请等待审核'
							})
							this.$refs.imgUpload.empt()
							this.$refs.imgUpload1.empt()
							this.emptForm()
						} else {
							//其他时候先缴费
							this.show = true;
						}
					}
				})
			},
			emptForm() {
				for (let k in this.formData) {
					this.formData[k] = ''
				}
			},
			upImgs(e) {
				// console.log(e)
				let arr = e.images
				this.formData.gacade_img = arr.join(',') //门头照
				//console.log(arr, this.formData);
			},
			upImgs1(e) {
				// console.log(e)
				let arr = e.images
				this.formData.business_img = arr.join(',') //营业执照
				//console.log(arr, this.formData);
			},
			goAuth() {
				console.log(this.formData)
				for (let k in this.formData) {
					if (this.formData[k] == '') {
						this.$refs.uToast.show({
							type: 'error',
							title: '请完善相关信息',
							message: "请完善相关信息",
						})
						return false;
					}
				}
				if(this.is_nursery_certification==1){
					this.$refs.uToast.show({
						type: 'success',
						message: "您已完成苗圃信息认证，无需多次认证!",
					})
					return false;
				}
				this.upLoadData()
			},
			getConfig() {
				systemConfig().then(res => {
					// console.log(res)
					this.sysConfig = res.data
					this.content = `苗圃商家认证费用需要支付费用${this.sysConfig.sys_config.nursery_certificat}`
				})
			},
			getUser() {
				getUser().then(res => {
					// console.log(res)
					this.nurseryPay = res.data.is_nursery_pay //等于0时未支付
					this.is_nursery_certification = res.data.is_nursery_certification 
					this.showNursery = res.data.is_nursery_pay == 0 ? true : false
					// this.showNursery = res.data.is_nursery_pay ==true
				})
			}
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			// this.$refs.form1.setRules(this.rules)
			this.getConfig()
			this.getUser()
			this.getInfo()
		},
	}
</script>

<style lang="scss" scoped>
	.myAuth {
		padding: 20rpx 15rpx;
		display: flex;
		flex-direction: column;
	}
</style>
